package com.sys.role.controller;

import com.app.utils.Result;
import com.app.view.TreeMenu;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.aspect.Log;
import com.sys.menu.service.SysMenuService;
import com.sys.role.entity.SysRole;
import com.sys.role.service.SysRoleService;
import com.sys.role.view.SysRoleView;
import com.sys.rolemenu.entity.SysRoleMenu;
import com.sys.rolemenu.service.SysRoleMenuService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author djianfeng
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    SysRoleService sysRoleService;
    @Autowired
    SysMenuService menuService;
    @Autowired
    SysRoleMenuService roleMenuService;

    @Log("查询角色列表")
    @RequiresPermissions("RoleList")
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<SysRoleView> page, SysRoleView view) {
        Map<String, Object> map = new HashMap<>();
        List<SysRoleView> list = sysRoleService.getSysRoleViews(page, view);
        map.put("records", list);
        map.put("total", page.getTotal());
        return map;
    }

    @Log("进入角色管理")
    @RequiresPermissions("Role")
    @RequestMapping("/golist.do")
    public String tolist() {
        return "system/role/list";
    }

    @Log("查询角色")
    @RequiresPermissions("EditRole")
    @RequestMapping("/goEdit.do")
    public ModelAndView goEdit(SysRole entity) {
        Map map = new HashMap();
        if (entity.getId() != null) {
            entity = this.sysRoleService.selectById(entity.getId());
            map.put("role_id", entity.getId());
            List<SysRoleMenu> list = (List)roleMenuService.selectByMap(map);
            if (list != null && !list.isEmpty()) {
                Long[] menuIds = new Long[list.size()];
                for (int i = 0; i < list.size(); i++) {
                    menuIds[i] = list.get(i).getMenuId();
                }
                map.put("menuIds", menuIds);
            }
        }
        map.put("entity", entity);
        List<TreeMenu> treeMenus = menuService.selectAllMenu();
        map.put("treeMenus", treeMenus);
        return new ModelAndView("system/role/edit", map);
    }

    @Log("查看角色")
    @RequiresPermissions("EditRole")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public Result save(SysRoleView view) {
        boolean n = this.sysRoleService.save(view);
        return Result.returnResult(n);
    }

    @Log("删除角色")
    @RequiresPermissions("DelRole")
    @RequestMapping(value = "/del.do", method = RequestMethod.POST)
    @ResponseBody
    public Result del(SysRole entity) {
        boolean n = this.sysRoleService.deleteById(entity.getId());
        Map map = new HashMap();
        map.put("role_id", entity.getId());
        this.roleMenuService.deleteByMap(map);
        return Result.returnResult(n);
    }

    @Log("修改角色")
    @RequiresPermissions("UpdateRole")
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ResponseBody
    public Result update(SysRole entity) {
        boolean n = this.sysRoleService.updateById(entity);
        return Result.returnResult(n);
    }

}
